package com.fairytask.ui.petsUnit
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.SkillItem;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	/**
	 * 宠物属性UI 
	 * @author xujh
	 * 
	 */
	public class PetsProfileUI extends UnitUI
	{
		private static var _self		:PetsProfileUI;
		
		public var skillBG		:MovieClip;	
		public var curBG		:MovieClip;
		public var nextBG		:MovieClip;
		
		public var skillLab	:Label;		//宠物技能
		public var skillVG		:ViewGroup;	//技能容器
		public var curTxt		:Text;		//当前描述
		public var nextTxt		:Text;		//下阶描述
		public var upStagBtn	:Button;	//升阶按钮
		
		private var _unbind	:Array = [];	//绑定
		private var _skillList	:Array = [];	//技能列表
		
		public static function getInstance():PetsProfileUI
		{
			if (_self == null)
			{
				_self=new PetsProfileUI;
			}
			return _self;
		}
		
		public function PetsProfileUI()
		{
			super();
			initEvent();
			initSkill();
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			upStagBtn.addEventListener(MouseEvent.CLICK,onUpStag);
			//技能tips
			skillVG.addItemsEvent(MouseEvent.MOUSE_OVER,onOver);
			skillVG.addItemsEvent(MouseEvent.MOUSE_OUT,onOut);
		}
		
		/**
		 * 初始化技能 
		 * 
		 */
		private function initSkill():void
		{
			for(var i:int=0;i<10;i++)
			{
				var tmpItem		:SkillItem = new SkillItem();
				var baseItem	:BaseItem = skillVG.getItemByIndex(i);
				tmpItem.pos = i;
				tmpItem.data = null;
				tmpItem.skillId = null;
				baseItem.addItem(tmpItem);
			}
		}
		
		/**
		 * 切换宠物 
		 * @param val
		 * 
		 */
		public function selectPet(val:ViewObject):void
		{
			//解绑数据
			for each(var tmpBind:Array in _unbind)
			{
				bindManager.UnbindProp(tmpBind.shift(),tmpBind.shift(),tmpBind.shift());
			}
			var str	:String = val.ObjKey;
			_unbind.splice(0);
			//绑定当前规则
			/*_unbind.push([val,"PStagRule",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_PET),str,"PStagRule",pStagRule,null,[str])]);
			_unbind.push([val,"NStagRule",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_PET),str,"NStagRule",nStagRule,null,[str])]);
			_unbind.push([val,"Stag",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_PET),str,"Stag",rStag,null,[val])]);*/
		}
		
		/**
		 * 当前规则
		 * @param key
		 * @param skillID
		 * @param pList
		 * 
		 */
		private function pStagRule(key:String,val:int,pList:Array):void
		{
			curTxt.htmlStr = StaticIni.getSwfLangStrVar("1000000224",getStag(val,pList[0]));
		}
		
		/**
		 * 下阶规则
		 * @param key
		 * @param skillID
		 * @param pList
		 * 
		 */
		private function nStagRule(key:String,val:int,pList:Array):void
		{
			if(val == 0)
			{
				nextTxt.htmlStr = "";
				return;
			}
			nextTxt.htmlStr = StaticIni.getSwfLangStrVar("1000000225",getStag(val,pList[0]));
		}
		
		/**
		 * 街数 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function rStag(key:String,val:int,pList:Array):void
		{
			//清除技能
			clearVG();
			_skillList = [];
			for(var i:int = 1; i < val + 1;i++)
			{
				var tmpkey	:String = "Skill" + i;
				if(pList[0].CheckProperties(tmpkey))
				{
					_skillList.push(pList[0].GetProperties(tmpkey));
				}
			}
			addPetSkill();
		}
		
		/**\
		 * 添加宠物技能
		 * 
		 */
		private function addPetSkill():void
		{
			var tmpItem	:SkillItem;
			for(var i:int=0; i<_skillList.length; i++)
			{
				tmpItem = skillVG.getItemByIndex(i).getItem() as SkillItem;
				tmpItem.source = ResConfig.iconPath +StaticIni.getIniVar(_skillList[i],"Icon") + ResConfig.itemImg;
				tmpItem.skillId = _skillList[i];
			}
		}
		
		/**
		 * 根据id取内容 
		 * @param id
		 * @return 
		 * 
		 */
		private function getStag(val:int,obj:String):Array
		{
			var arr		:Array = [];
			//服务端初始宠物规则0
			if(val == 0)
			{
				val = 15020001;
			}
			var stag	:String = StaticIni.getIniVar(val,"Stag");
			//属性
			arr.push(stag);
			//敏捷
			arr.push(StaticIni.getIniVar(val,"Dex"));
			//力量
			arr.push(StaticIni.getIniVar(val,"Str"));
			//智力
			arr.push(StaticIni.getIniVar(val,"Spi"));
			//智力
			arr.push(StaticIni.getIniVar(val,"Ing"));
			//体质
			arr.push(StaticIni.getIniVar(val,"Sta"));
//			//技能
			arr.push(getProp(obj,stag));
			return arr;
		}
		
		/**
		 * 获取属性 
		 * @param obj
		 * @param key
		 * @return 
		 * 
		 */
		private function getProp(obj:String,stag:String):String
		{
			var str :String = "Skill" + stag;
			/*if((model.scene.GetView(ViewDefine.VIEWPORT_PET).GetPropertiesEx(obj) as ViewObject).CheckProperties(str))
			{
				var skillID	:int = (model.scene.GetView(ViewDefine.VIEWPORT_PET).GetPropertiesEx(obj) as ViewObject).GetPropertiesEx(str);
				str = StaticIni.getSwfLangStr(StaticIni.getIniVar(skillID,"NameID"));
			}
			else
			{
				str = "";
			}*/
			return str;
		}
		
		/**
		 * 清除宠物技能 
		 * 
		 */
		private function clearVG():void
		{
			for(var i:int=0; i<_skillList.length; i++)
			{
				clearSkill(i);
			}
		}
		
		/**
		 * 清除技能，并加锁 
		 * @param pos
		 * 
		 */
		private function clearSkill(pos:int):void
		{
			var tmpItem	:SkillItem = skillVG.getItemByIndex(pos).getItem() as SkillItem;
			if(null == tmpItem.skillId)
			{
				return;
			}
			//清除内容
			tmpItem.skillId = null;
			//清除图片
			tmpItem.clearPhoto();
			/*if(vg == skillVG)
			{
				//锁定
				tmpItem.lockSkill = true;
			}*/
		}
		
		/**
		 * 升阶 
		 * @param evt
		 * 
		 */
		private function onUpStag(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GROWSTAG,PetsListUI.getInstance().curObj);
		}
		
		/**
		 * 显示tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onOver(evt:MouseEvent,pos:int):void
		{
			var tmpItem	:SkillItem = skillVG.getItemByIndex(pos).getItem() as SkillItem;
			if(null != tmpItem.skillId)
			{
				TipsManage.showSkillTips(uint(tmpItem.skillId));
			}
		}
		
		/**
		 * 隐藏tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onOut(evt:MouseEvent,pos:int):void
		{
			ToolTips.hide();
		}
	}
}